package c.x.jy.service.service.impl;

import c.x.jy.common.exception.RRException;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;
import c.x.jy.dao.dao.MemberCashApplyDao;
import c.x.jy.dao.dto.MemberCashApplyEntityDto;
import c.x.jy.dao.entity.MemberCashApplyEntity;
import c.x.jy.dao.entity.MemberEntity;
import c.x.jy.dao.entity.MemberWalletEntity;
import c.x.jy.dao.entity.SysUserEntity;
import c.x.jy.service.config.BusinessConfigService;
import c.x.jy.service.service.MemberCashApplyService;
import c.x.jy.service.service.MemberService;
import c.x.jy.service.service.MemberWalletService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;


@Service("memberCashApplyService")
public class MemberCashApplyServiceImpl extends ServiceImpl<MemberCashApplyDao, MemberCashApplyEntity> implements MemberCashApplyService {
    @Autowired
    MemberService memberService;
    @Autowired
    BusinessConfigService businessConfigService;
    @Autowired
    MemberWalletService memberWalletService;
//    @Autowired
//    MemberWallelogService memberWallelogService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<MemberCashApplyEntity> query= 	 new QueryWrapper<MemberCashApplyEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
//        IPage<MemberCashApplyEntityDto> page = this.page(
//                new Query<MemberCashApplyEntity>().getPage(params),
//                 query
//        );
        query.eq("member_id",memberService.getCurrUser().getId());

        IPage<MemberCashApplyEntityDto> page =   this.getBaseMapper().querypage(new Query<MemberCashApplyEntity>().getPage(params),  query);

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void getcash(MemberCashApplyEntity memberCashApply) {
        MemberEntity memberEntity = memberService.getCurrUser();
        memberCashApply.setMemberId(memberEntity.getId());
        memberCashApply.setApplyUserId(memberEntity.getId());
        String member_cash_feerate = businessConfigService.getBusinessConfigValue(null,"MEMBER_CASH_FEERATE");

        memberCashApply.setChargeFee(new BigDecimal(member_cash_feerate).multiply(memberCashApply.getCashAmount()));
        memberCashApply.setActualAmount(memberCashApply.getCashAmount().subtract(memberCashApply.getChargeFee()));
        memberCashApply.setStatus(1);
        memberCashApply.setDescription(memberEntity.getNickname()+"-"+memberEntity.getName());

        MemberWalletEntity memberWalletEntity = memberWalletService.selectMemberWalletByIdFOrUpdate(memberEntity.getId());
        if(memberWalletEntity==null){
            throw new RRException("当前用户没有钱包，请联系管理员");
        }
        System.out.println(memberCashApply.getCashAmount()+"-"+memberWalletEntity.getCommissionBalance());
        if(memberCashApply.getCashAmount().compareTo(memberWalletEntity.getCommissionBalance())>0){
            throw new RRException("余额不足");
        }

        save(memberCashApply);

        memberWalletEntity.setCommissionBalance(memberWalletEntity.getCommissionBalance().subtract(memberCashApply.getCashAmount()));
        memberWalletEntity.setFrozenCommission(memberWalletEntity.getFrozenCommission().add(memberCashApply.getCashAmount()));

        memberWalletService.updateById(memberWalletEntity);
//        MemberWallelogEntity memberWallelogEntity=new MemberWallelogEntity();
//        memberWallelogEntity.set
    }

    @Override
    public void passById(Long id, SysUserEntity checkUser) {
        MemberCashApplyEntity memberCashApplyEntity = getBaseMapper().selectById(id);
        memberCashApplyEntity.setCheckUserId(checkUser.getUserId());
        memberCashApplyEntity.setStatus(2);
        memberCashApplyEntity.setCheckDate(new Date());
        getBaseMapper().updateById(memberCashApplyEntity);
    }

    @Override
    public void rejectById(Long id, SysUserEntity checkUser) {
        MemberCashApplyEntity memberCashApplyEntity = getBaseMapper().selectById(id);
        memberCashApplyEntity.setCheckUserId(checkUser.getUserId());
        memberCashApplyEntity.setStatus(-1);
        memberCashApplyEntity.setCheckDate(new Date());
        getBaseMapper().updateById(memberCashApplyEntity);
    }

    @Override
    public void payById(Long id, SysUserEntity checkUser) {
        MemberCashApplyEntity memberCashApplyEntity = getBaseMapper().selectById(id);
        memberCashApplyEntity.setCashUserId(checkUser.getUserId());
        memberCashApplyEntity.setStatus(3);
        memberCashApplyEntity.setCashDate(new Date());
        getBaseMapper().updateById(memberCashApplyEntity);
    }

    @Override
    public PageUtils queryPageforWeb(Map<String, Object> params) {
        QueryWrapper<MemberCashApplyEntity> query= 	 new QueryWrapper<MemberCashApplyEntity>() ;
        if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
            query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));


        IPage<MemberCashApplyEntityDto> page =   this.getBaseMapper().querypage(new Query<MemberCashApplyEntity>().getPage(params),  query);

        return new PageUtils(page);
    }
}
